<?php
error_reporting ( E_ALL ) ;
ini_set ( 'display_errors' , 1 ) ;

include "../settings.php";
include "../include/init.php";
include "../include/functions.php";

$message = "";

if (isset($_POST['user']) || isset($_GET['instance'])){ 
	$instance = (isset($_POST['user']) ? htmlentities($_POST['user']) : htmlentities($_GET['instance']));
	$check = json_decode(file_get_contents("https://$instance/api/v1/instance"),true);
	
	if(is_null($check['title'])){
		$message = "<p>There was a problem when fetching info from the instance, make sure you wrote the url correctly and without trailing slashes.</p>";
	} else {
		$instance_list = scandir("../data/");
		
		$cdata = parse_url($setting['url']);

		if(!in_array("$instance.php",$instance_list)){
			$client = register_app($instance);
			file_put_contents("../data/$instance.php",'<?php $client = '.var_export($client,true).';');
			if (!file_exists("../data/$instance.php")){
				echo "<p style='color:red; font-weight:bold;'>Unable to save instance details, please verify that the 'data' folder has writrable permissions.</p>";
				die();
			}
		} else {
			include "../data/$instance.php";
		}
		
		$user_settings['replies'] = "on";
		$user_settings['instance'] = "$instance";
		$user_settings['text'] = "on";
		$user_settings['reblog'] = "on";		
		$user_settings['explicit'] = "blur";
		$user_settings['videoloop'] = "off";
		setrawcookie("user_settings",base64_encode(json_encode($user_settings)),time()+60*60*24*30,'/');
		
		header('Location: '."https://$instance/oauth/authorize?response_type=code&client_id=".$client['client_id']."&redirect_uri=".$client['redirect_uri']."&scope=read+write+follow");
	}
} 

$bg = scandir("./bg/");
$bgfile = $bg[mt_rand(2,count($bg)-1)];
@$exif = exif_read_data("./bg/".$bgfile, 'COMMENT')['COMMENT'][0];
?>
<!DOCTYPE html>
<html>
<head>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link href="https://fonts.googleapis.com/css?family=Patua+One&display=swap" rel="stylesheet"> 
	<style type="text/css">
@font-face {
      font-family: 'fontello';
      src: url('../font/fontello.eot?43704775');
      src: url('../font/fontello.eot?43704775#iefix') format('embedded-opentype'),
           url('../font/fontello.woff?43704775') format('woff'),
           url('../font/fontello.ttf?43704775') format('truetype'),
           url('../font/fontello.svg?43704775#fontello') format('svg');
      font-weight: normal;
      font-style: normal;
    }
	
	html {
		background: url(bg/<?php echo $bgfile; ?>) no-repeat center center fixed; 
		-webkit-background-size: cover;
		-moz-background-size: cover;
		-o-background-size: cover;
		background-size: cover;
	}
	
	.bginfo {
		text-align:left;
		color:white;
		font-family:sans-serif;
		font-size:10px;
	}
	
	.bginfo a {
		color:white;
		font-weight:bold;
	}
	</style>
</head>
<body >
<div style='width:100vw; height:100%; display:inline-block; vertical-align:middle; text-align:center;'>
<div class="bginfo"><?php echo $exif; ?></div>

<div style='width:300px; height:200px; margin-top:10%; background-color:rgba(0, 0, 0, 0.5); display:inline-block; padding:30px; border-radius:3px;'>
	<span style="font-family: 'Patua One', serif; font-weight:bold; color:white; font-size:45px; text-shadow: 2px 2px #000; ">Dashboard FE</span>
	
	<?php echo $message; ?>
	<form method="post">
		<input type="text" name="user" placeholder="your.instance.url" style='width:200px height:30px; font-size:20px; border: 2px solid darkgray; padding:5px;'>
		<input type="submit" style='background-color:#203960; padding:10px; border: 2px solid white; margin: 10px; font-family: fontello, sans serif; color:white; font-weight:bold; font-size:20px; border-radius:3px;' value="Login &#xe832;">
	</form>
</div>
</div>
</body>
